package cn.donting.jme.expansion.attribute;


import cn.donting.jme.swing.layout.Anchor;
import cn.donting.jme.swing.layout.AnchorLayout;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;

public class BaseInfoAttributeGroup extends JPanel {

    public static final int ICON_SIZE = 50;

    private JTextField nameTextField = new JTextField();
    private JLabel icon = new JLabel();
    private JLabel type = new JLabel();

    private ValueChangeListener<String> nameChange;
    private ValueChangeListener<Boolean> enabledChange;
    private JCheckBox enabledCheckBox;

    public BaseInfoAttributeGroup() {
        setLayout(new AnchorLayout());
//        jPanel.setPreferredSize(new Dimension(200,150));
        setSize(new Dimension(200, 60));
        icon.setPreferredSize(new Dimension(ICON_SIZE, ICON_SIZE));
        add(nameTextField, new Anchor(7, 60, 5, null));
        enabledCheckBox = new JCheckBox();
        enabledCheckBox.setEnabled(false);
        enabledCheckBox.setSelected(true);
        add(icon, new Anchor(7, 7, null, null));
        add(type, new Anchor(37, 80, 5, null));
        add(enabledCheckBox, new Anchor(37, 60, null, null));
        type.setText("type");
        nameTextField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                if (nameChange != null) {
                    nameChange.change(nameTextField.getText());
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                insertUpdate(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });
        enabledCheckBox.addChangeListener(e -> {
            if (enabledChange!=null) {
                enabledChange.change(enabledCheckBox.isSelected());
            }
        });
    }

    public void setName(String name) {
        nameTextField.setText(name);
    }

    public String getName() {
        return nameTextField.getText();
    }

    public void setType(String type) {
        this.type.setText(type);
        this.type.setToolTipText(type);
    }

    public void setIcon(Icon icon) {
        this.icon.setIcon(icon);
    }

    public void setEnabledCheckBox(boolean enabled){
        this.enabledCheckBox.setEnabled(enabled);
    }
    public void setEnabledCheckBoxSelect(boolean selected){
        this.enabledCheckBox.setSelected(selected);
    }
    public void setNameChange(ValueChangeListener<String> nameChange) {
        this.nameChange = nameChange;
    }
    public void setEnabledChange(ValueChangeListener<Boolean> nameChange) {
        this.enabledChange = nameChange;
    }
}
